#define  _CRT_SECURE_NO_WARNINGS 1  
#include"Sort.h"

void PrintArray(int* arr, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d", arr[i]);
	}
	printf("\n");
}
void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				--end;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}
void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				--end;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}
void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				--end;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}
void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end];
		while (end >= 0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				--end;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}
void InsertSort(int* arr, int n)
{
	assert(arr);
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				--end;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}
void ShellSort(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (tmp < arr[end])
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}
void ShellSort(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (tmp < arr[end])
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}
void ShellSort(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (tmp < arr[end])
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					end -= gap;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}
void Swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
void AdjustDown(int* arr, int size, int parent)
{
	int child = parent * 2 + 1;
	while (child < size)
	{
		if (child + 1 < size && arr[child + 1] > arr[child])
		{
			child++;
		}
		if (arr[child] > arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
void HeapSort(int* arr, int n)
{
	for (int i = n - 1 - 1 / 2; i >= 0; i++)
	{
		AdjustDown(arr, n, i);
	}
}
void SelectSort(int* arr, int n)
{
	assert(arr);
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int mini = begin;
		int maxi = begin;
		for (int i = begin + 1; i <= end; i++)
		{
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
		}
		Swap(&arr[mini], &arr[begin]);
		if (begin == maxi)
		{
			maxi = mini;
		}
		Swap(&arr[maxi], &arr[end]);
		++begin;
		--end;
	}
}
void SelectSort(int* arr, int n)
{
	assert(arr);
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int mini = begin;
		int maxi = begin;
		for (int i = begin + 1; i <= end; i++)
		{
			if (arr[i] >= arr[maxi])
			{
				maxi = i;
			}
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
		}
		Swap(&arr[mini], &arr[begin]);
		if (begin == mini)
		{
			maxi = mini;
		}
		Swap(&arr[maxi], &arr[end]);
		++begin;
		--end;
	}
}
void SelectSort(int* arr, int n)
{
	assert(arr);
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int mini = begin;
		int maxi = begin;
		for (int i = begin + 1; i <= end; i++)
		{
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
		}
		Swap(&arr[mini], &arr[begin]);
		if (begin == maxi)
		{
			maxi = mini;
		}
		Swap(&arr[maxi], &arr[end]);
		++begin;
		--end;
	}
}
void BubbleSort(int* arr, int n)
{
	int flag = 0;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i; j < n - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				Swap(&arr[j], &arr[j + 1]);
				flag = 1;
			}
		}
		if (flag == 0)
		{
			break;
		}
	}
}
void BubbleSort(int* arr, int n)
{
	int flag = 0;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i; j < n - i - 1; j++)
		{
			if (arr[j + 1] > arr[j])
			{
				Swap(&arr[j + 1], &arr[j]);
				flag = 1;
			}
		}
		if (flag == 0)
		{
			break;
		}
	}
}
void QuickSort(int* arr, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int left = begin;
	int right = end;
	int key = begin;
	while (begin < end)
	{
		if (begin < end && arr[right] > arr[key])
		{
			right--;
		}
		if (begin < end && arr[left] < arr[key])
		{
			left++;
		}
		Swap(&arr[left], &arr[right]);
		key = left;

		QuickSort(arr, begin, key - 1);
		QuickSort(arr, key, end);
	}
}
void QuickSort(int* arr, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int left = begin;
	int right = end;
	int key = begin;
	while (left < right)
	{
		while (left < right && arr[left] < arr[key])
		{
			left++;
		}
		while (left<right && arr[right]>arr[key])
		{
			right--;
		}
		Swap(&arr[left], &arr[right]);
		key = left;

		QuickSort(arr, begin, key - 1);
		QuickSort(arr, key + 1, end);
	}
}
void QuickSort(int* arr, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int left = begin;
	int right = end;
	int key = begin;
	while (left < right)
	{
		if (left<right && arr[right]>arr[key])
		{
			--right;
		}
		if (left < right && arr[left] < arr[key])
		{
			++left;
		}
		Swap(&arr[left], &arr[right]);
		key = left;

		QuickSort(arr, begin, key - 1);
		QuickSort(arr, key + 1, end);
	}
}
void QuickSort(int* arr, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int left = begin;
	int right = end;
	int key = begin;
	while (begin < end)
	{
		if (begin<end && arr[right]>arr[key])
		{
			--right;
		}
		if (begin < end && arr[left] < arr[key])
		{
			++left;
		}
		Swap(&arr[left], &arr[right]);
		key = left;

		QuickSort(arr, begin, key - 1);
		QuickSort(arr, key + 1, end);
	}
}
void QuickSort(int* arr, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int left = begin;
	int right = end;
	int key = left;
	while (left < right)
	{
		while (left < right && arr[left] >= arr[key])
		{
			right--;
		}
		while (left<right && arr[left]>arr[key])
		{
			left++;
		}
		Swap(&arr[left], &arr[right]);
	}
	Swap(&arr[key], &arr[left]);
	key = left;
	QuickSort(arr, begin, key - 1);
	QuickSort(arr, key + 1, end);
}